package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pojo.ShoppingCart;
import com.service.ShoppingCartService;
import com.util.BaseContext;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShopingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> getShop(){
        //Long aLong = (Long) request.getSession().getAttribute("employee");
        Long aLong = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,aLong);
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> deleted(){
        //Long aLong = (Long) request.getSession().getAttribute("employee");
        Long aLong = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,aLong);
        shoppingCartService.remove(wrapper);
        return R.success("删除成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //Long aLong = (Long) request.getSession().getAttribute("employee");
        Long aLong = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(null!=shoppingCart.getDishId(),ShoppingCart::getDishId,shoppingCart.getDishId());
        wrapper.eq(null!=shoppingCart.getSetmealId(),ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        wrapper.eq(ShoppingCart::getUserId,aLong);
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        Integer number = one.getNumber();
        if (number==1){
            shoppingCartService.removeById(one);
        }else {
            one.setNumber(number-1);
            shoppingCartService.updateById(one);
        }
        return R.success("减少成功");
    }


    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        //Long aLong = (Long) request.getSession().getAttribute("employee");
        Long aLong = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getName,shoppingCart.getName());
        wrapper.eq(ShoppingCart::getUserId,aLong);
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if (one!=null) {
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        }else {
            shoppingCart.setUserId(aLong);
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加成功");
    }
}
